package org.ibase4j.assistant;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;



public class CrawlingZhongZhengUtil {
	public static Map<String, Object> collect(int test) throws MalformedURLException {
		final int FiveMinutes = 1;
		final int FifteenMinutes = 2;
		final int ThirtyMinutes = 3;
		final int SixtyMinutes = 4;
		Map<String, Object> resultMap = null;
		switch (test) {
		case FiveMinutes:
			System.out.println("FiveMinutes");
			resultMap = getValue("http://money.finance.sina.com.cn/quotes_service/api/jsonp_v2.php/var%20_sh000905_30_1499416801384=/CN_MarketData.getKLineData?symbol=sh000905&scale=5&ma=no&datalen=1023");
			break;
		case FifteenMinutes:
			System.out.println("FifteenMinutes");
			resultMap = getValue("http://money.finance.sina.com.cn/quotes_service/api/jsonp_v2.php/var%20_sh000905_30_1499416801384=/CN_MarketData.getKLineData?symbol=sh000905&scale=15&ma=no&datalen=1023");
			break;
		case ThirtyMinutes:
			System.out.println("ThirtyMinutes");
			resultMap = getValue("http://money.finance.sina.com.cn/quotes_service/api/jsonp_v2.php/var%20_sh000905_30_1499416801384=/CN_MarketData.getKLineData?symbol=sh000905&scale=30&ma=no&datalen=1023");
			break;
		case SixtyMinutes:
			System.out.println("SixtyMinutes");
			resultMap = getValue("http://money.finance.sina.com.cn/quotes_service/api/jsonp_v2.php/var%20_sh000905_30_1499416801384=/CN_MarketData.getKLineData?symbol=sh000905&scale=60&ma=no&datalen=1023");
			break;
		default:
			System.out.println(test);
			break;
		}
		return resultMap;
	}

	private static Map<String, Object> getValue(String strURL) {
		URL url;
		Map<String, Object> valueMap = new HashMap<String, Object>();
		try {
			url = new URL(strURL);
			URLConnection context = url.openConnection();
			InputStream in = context.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in, "gbk"));
			String line = "";
			StringBuilder contentBuf = new StringBuilder();
			while ((line = br.readLine()) != null) {
				contentBuf.append(line);
			}
			String buf = contentBuf.toString();
			String buf1 = buf.replace("var _sh000905_30_1499416801384=([", ",");
			String buf2 = buf1.replace("])", "");
			String[] vStrings = buf2.split("}");
			System.out.println(vStrings[vStrings.length - 1]);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String ymdString= vStrings[vStrings.length - 1].substring(7, 26);
			Date ymd = sdf.parse(ymdString);
			Timestamp tiem = new Timestamp(ymd.getTime());
			double openv = Double.parseDouble(vStrings[vStrings.length - 1].substring(34, 42));
			double high = Double.parseDouble(vStrings[vStrings.length - 1].substring(50, 58));
			double low = Double.parseDouble(vStrings[vStrings.length - 1].substring(65, 73));
			double close = Double.parseDouble(vStrings[vStrings.length - 1].substring(82, 90));
			valueMap.put("openv", openv);
			valueMap.put("high", high);
			valueMap.put("low", low);
			valueMap.put("close", close);
			valueMap.put("ymd", tiem);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return valueMap;
	}
	public static Map<String, Object> collectRealTime(){
		Map<String, Object> crawlingHtmlMap = crawlingHtml("http://hq.sinajs.cn/etag.php?_=1498530357177&list=sh000905");
		return crawlingHtmlMap;
	}
	
	private static Map<String , Object> crawlingHtml(String strURL) {
		URL url;
		Map<String, Object> crawValue= new HashMap<String, Object>();
		try {
			url = new URL(strURL);
			URLConnection context = url.openConnection();
			InputStream in = context.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in, "gbk"));
			String line = "";
			StringBuilder contentBuf = new StringBuilder();
			while ((line = br.readLine()) != null) {
				contentBuf.append(line);
			}
			String buf = contentBuf.toString();
			String[] kline = buf.split(",");
			String open = kline[1];
			String high = kline[4];
			String close = kline[3];
			String low = kline[5];
			String ymdString = kline[30];
			String ymdString2= kline[31];
			String ymdall = ymdString+" "+ymdString2;
			double openvalue = Double.valueOf(open);
			double highvalue = Double.valueOf(high);
			double closevalue = Double.valueOf(close);
			double lowvalue = Double.valueOf(low);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date ymd = sdf.parse(ymdall);
			Timestamp tiem = new Timestamp(ymd.getTime());
			crawValue.put("openv", openvalue);
			crawValue.put("high", highvalue);
			crawValue.put("low", closevalue);
			crawValue.put("close", lowvalue);
			crawValue.put("ymd", tiem);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return crawValue;
		
	}

	public static void main(String[] args) throws MalformedURLException {
		Map<String, Object> collect = CrawlingZhongZhengUtil.collectRealTime();
		System.out.println(collect.get("ymd"));
	}
}
